<?php
/*
 * 2021/7/16
 * 17:18
 * User: 2297848261@qq.com
 */
namespace App\Api\Repositories;

class CategoryRepositorie
{
    /**
     * 无限极分类
     * @param $data 所有数据
     * @param $pid 父级id
     * @return array
     */
    public static function getTreeList($data)
    {
        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];

        foreach($data as $v){
            $v['son'] = [];

            $list[$v['category_id']] = $v;
        }
        //获取分类树
        foreach($list as $k=>$v){
            $list[$v['pid']]['son'][] = &$list[$v['category_id']];
        }
        return isset($list[0]['son']) ? $list[0]['son'] : [];
    }
}